const app = getApp()
var util = require('../../utils/util.js')
var address = require('../../utils/city.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    isNotOpen:false
  },

  // 打开礼物
  open: function () {
   
    let that=this
    console.log(that.data.giftId)
     if (that.data.giftId){
          if (this.data.userInfo.isLogin != 1) {
               wx.showModal({
                    title: '提示',
                    content: '小程序需要您的微信授权才能使用哦',
                    success: function (res) {
                         wx.redirectTo({
                              url: '../bind/bind?orderId=' + that.data.orderId
                         })
                    }
               })
          } else {
               this.setData({
                    openSta: true,
                    isNotOpen: false
               })
          }
     }else{
          // wx.showToast({
          //      title: "已被领取",
          //      icon: 'none',
          //      duration: 1500
          // })
     }
       
  },

     addressNameInput: function (e) {
          this.setData({
               addressName: e.detail.value
          })
     },
     //收货人手机号
     addressMobileInput: function (e) {
          this.setData({
               addressMobile: e.detail.value
          })
     },
     //详细地址
     addressDescInput: function (e) {
          this.setData({
               addressDesc: e.detail.value
          })
     },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      // 初始化动画变量
      var animation = wx.createAnimation({
        duration: 500,
        transformOrigin: "50% 50%",
        timingFunction: 'ease',
      })
      this.animation = animation;
      // 默认联动显示北京
      var id = address.provinces[0].id
      this.setData({
        provinces: address.provinces,
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })


       let that = this
       this.setData({
            orderId: options.orderId
       })
       that.loadOrder()
       if (options.giftId){
            this.setData({
                 giftId: options.giftId
            }) 
       }
       console.log(that.data.giftId)
       if (app.globalData.userInfo && app.globalData.userInfo != '') {
            this.setData({
                 albumDisabled: false,
                 bindDisabled: true,
                 userInfo: app.globalData.userInfo
            });
            console.log('orderId='+that.data.orderId)
            console.log('memberId=' + that.data.userInfo.id)
             //领取礼物
             that.reciveGift()
            
       } else {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = userInfo => {
                 this.setData({
                      userInfo: userInfo
                 })
                 console.log('orderId=' + that.data.orderId)
                 console.log('memberId=' + that.data.userInfo.id)
                 //领取礼物
                 that.reciveGift()
            }
       }
  },
  loadOrder:function(){
       let that = this
       var data = { 'orderId': that.data.orderId }
       var promise = app.synPostData('order/findOne', data)
       promise.then(function (res) {
            that.setData({
                 order:res.data.result
            })
       }) 
  },
  reciveGift:function(){
       let that=this
       var data = { 'orderId': that.data.orderId,'receiveId':app.globalData.userInfo.id}
       var promise = app.synPostData('order/receiveGift', data)
       promise.then(function (res) {
            if (res.data.code==11){
                 wx.showToast({
                         title: "已被领取",
                         icon: 'none',
                         duration: 1500
                    })
            } else if (res.data.code == 0){
                 if(res.data.result){
                      that.setData({
                           giftId: res.data.result
                      })
                 }
            }
       })
  },
  openGift:function(){
       let that = this
         
       let receiveName = this.data.addressName
       if (util.isBlank(receiveName)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 receiveName = this.data.defaultAddress.receiveName
            }
            if (util.isBlank(receiveName)) {
                 wx.showToast({
                      title: '请输入姓名',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }

       let receiveMobile = this.data.addressMobile
       if (util.isBlank(receiveMobile)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 receiveMobile = this.data.defaultAddress.receiveMobile
            }
            if (util.isBlank(receiveMobile)) {
                 wx.showToast({
                      title: '请输入电话',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }
       if (!util.validatemobile(receiveMobile)) {
            return
       }
       let receiveAddress = this.data.addressDesc
       if (util.isBlank(receiveAddress)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 receiveAddress = this.data.defaultAddress.receiveAddress
            }
            if (util.isBlank(receiveAddress)) {
                 wx.showToast({
                      title: '请输入详细地址',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }

       let provinceName = that.data.provinceName
       let provinceId = that.data.provinceId
       if (util.isBlank(provinceName)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 provinceName = this.data.defaultAddress.provinceName
            }
            if (util.isBlank(provinceName)) {
                 wx.showToast({
                      title: '请选择省份',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }
       let cityName = that.data.cityName
       let cityId = that.data.cityId
       if (util.isBlank(cityName)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 cityName = this.data.defaultAddress.cityName
            }
            if (util.isBlank(cityName)) {
                 wx.showToast({
                      title: '请选择城市',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }
       let districtName = that.data.districtName
       let districtId = that.data.districtId
       if (util.isBlank(districtName)) {
            if (!util.isBlank(this.data.defaultAddress)) {
                 districtName = this.data.defaultAddress.districtName
            }
            if (util.isBlank(cityName)) {
                 wx.showToast({
                      title: '请选择区县',
                      icon: 'none',
                      duration: 1500
                 })
                 return
            }
       }
       //设置不能点击
       this.setData({
            isNotOpen: true
       })
       var data = { 'giftId': that.data.giftId, 'receiveName': receiveName, 'receiveMobile': receiveMobile, 'provinceId': provinceId, 'provinceName': provinceName, 'cityId': cityId, 'cityName': cityName, 'districtId': districtId, 'districtName': districtName, 'receiveAddress': receiveAddress}
       var promise = app.synPostData('order/openGift', data)
       promise.then(function (res) {
            if (res.data.code != 0) {
                 wx.showToast({
                      title: '系统错误',
                      icon: 'none',
                      duration: 1500
                 })
            } else {
                 wx.redirectTo({
                      url: '../open-gift-success/open-gift-success'
                 })
            } 
       })
  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    var areaInfoId = that.data.provinces[value[0]].id + ',' + that.data.citys[value[1]].id + ',' + that.data.areas[value[2]].id
    that.setData({
      areaInfo: areaInfo,
      areaInfoId: areaInfoId,
      provinceName: that.data.provinces[value[0]].name,
      cityName: that.data.citys[value[1]].name,
      districtName: that.data.areas[value[2]].name,
      provinceId: that.data.provinces[value[0]].id,
      cityId: that.data.citys[value[1]].id,
      districtId: that.data.areas[value[2]].id
    })
  },
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
})